<template>
  <div id="app">
    <van-nav-bar
      v-if="titleNav"
      :title="titleNav"
      left-arrow
      @click-left="onClickLeft"
    />
    <router-view></router-view>
    <TabBar v-if="ShowTabBar"></TabBar>
    <!-- <TabBar></TabBar> -->
  </div>

  <!-- 回到顶部 -->
  <van-back-top bottom="100" />
</template>

<script setup>
import { watch, ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getMyUserInfo } from "./api/user";
import useUserInfo from "@/store/useUserInfo";
import { getToken } from "./utils/token";
import useCartInfo from "@/store/useCartInfo";

// 获取用户仓库实例
let userStore = useUserInfo();
// 获取购物车仓库实例
let cartStore = useCartInfo();

let ShowTabBar = ref(true);

// 获取当前 路由实例 路由跳转时 route会变化
let route = useRoute();
let router = useRouter();
let titleNav = ref("");
let onClickLeft = () => {
  router.back();
};
// 监听器
watch(
  // 监听路由发生变化
  route,
  (newRoute) => {
    // 如果路由发生变化 控制底部tabBar是否显示
    ShowTabBar.value = newRoute.meta.isShowNav;
    // 以及页面上面标题的变化
    titleNav.value = newRoute.meta.titleNav;
  },
  {
    immediate: true,
  }
);
// 页面刷新时
onMounted(() => {
  // 调用用户信息接口 把数据塞到仓库
  // 本地有token信息 调用仓库action 内部调用用户信息接口直接存储到仓库
  if (getToken()) {
    userStore.getUserInfo();
    cartStore.getCartInfo();
  }
});
</script>

<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>
